<template>
  <div class="register-container">
    <NuxtLink to="/">
      <span class="icon-close iconfont close" />
    </NuxtLink>
    <div class="header flex flex-column justify-center align-center">
      <span class="logo" />
      <div class="f-16">
        注册博MOCK
      </div>
      <div class="f-14">
        分享生活 发现世界
      </div>
    </div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="phone"
        clearable
        :error="false"
        name="phone"
        label=""
        placeholder="输入手机号"
        :rules="[
          { required: true, message: '请输入手机号' },
          {
            required: true,
            validator: phoneValidator,
            message: '请输入正确的手机号',
          },
        ]"
      />
       <van-field
        v-model="nickName"
        clearable
        :error="false"
        name="nickName"
        label=""
        placeholder="输入昵称"
        :rules="[
          { required: true, message: '请输入昵称' },
          {
            validator: nickNameValidator,
            message: '改昵称已存在',
            trigger:'onChange'
          }
        ]"
      />
      <van-field
        v-model="sms"
        center
        :error="false"
        clearable
        name="sms"
        placeholder="请输入短信验证码"
        :rules="[{ required: true, message: '请输入短信验证码' }]"
      >
        <template #button>
          <van-button size="small" type="warning" @click="getVcode">
            {{
              vcodeMsg
            }}
          </van-button>
        </template>
      </van-field>
      <van-field
        v-model="password"
        clearable
        :error="false"
        name="password"
        label=""
        type="password"
        placeholder="输入密码"
        :rules="[
          { required: true, message: '请输入密码' },
          {
            required: true,
            validator: passwordLengthValidator,
            message: '密码长度为至少为6位',
          },
        ]"
      />
      <van-field
        v-model="confirmPassword"
        clearable
        type="password"
        :error="false"
        name="confirmPassword"
        label=""
        placeholder="确认密码"
        :rules="[
          { required: true, message: '请输入确认密码' },
          {
            required: true,
            validator: passwordLengthValidator,
            message: '密码长度为至少为6位',
          },
          {
            required: true,
            validator: repeatPassword,
            message: '确认密码应与密码一致',
          },
        ]"
      />
      <van-button
        size="small"
        class="register-btn"
        round
        block
        ative-type="submit"
      >
        注册
      </van-button>
    </van-form>
    <NuxtLink to="/login">
      <van-button
        size="small"
        class="msg-login"
        round
        block
      >
        用短信验证码登录
      </van-button>
    </NuxtLink>
  </div>
</template>

<script>
import utils from "~/assets/js/util"
export default {
  data () {
    return {
      phone: '',
      password: '',
      nickName:"",
      sms: '',
      confirmPassword: '',
      vcodeMsg: '发送验证码',
      timer: null
    }
  },
  methods: {
    // 获取短信验证码
    getVcode () {
      if (this.timer) {
        return
      }
      let count = 60
      this.vcodeMsg = count + 's'
      this.timer = setInterval(() => {
        if (count <= 0) {
          this.vcodeMsg = '发送验证码'
          clearInterval(this.timer)
          this.timer = null
          return
        }
        count--
        this.vcodeMsg = count + 's'
      }, 1000)
      this.$once('hook:beforeDestroy', function () {
        clearInterval(this.timer)
        this.timer = null
      })
    },
    // 手机号校验
    phoneValidator (value, rule) {
      const reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
      return reg_tel.test(value)
    },
    //昵称校验
    async _nickNameValidator(value,rule){
      const res= await this.$axios.get("/v1/register/nickNameExist",{params:{nickName:value}})
      if(res&&res.data.code==200){
        if(res.data.result.nickNameExist){
          return false
        }
        return true
      }
      return false
    },
    // 密码长度校验
    passwordLengthValidator (value, rule) {
      return value.length >= 6
    },
    // 确认密码一致校验
    repeatPassword (value, rule) {
      return value === this.password
    },
    onSubmit (values) {
      console.log('submit', values)
      this.$axios.post('/v1/register/addUser', values).then((res) => {
        if (res && res.data.code == 200) {
          this.$router.replace('/')
          this.$store.commit('setLogin', true)
          this.$cookies.set("userInfo",res.data.result)
        }
      })
    }
  },
  created(){
    this.nickNameValidator=utils.debounce.call(this,this._nickNameValidator,1000)
  }
}
</script>

<style lang="scss" scoped>
.register-container {
  padding: 10px;
  .close {
    font-size: 25px;
  }
  .header {
    margin-bottom: 50px;
    .logo {
      margin-bottom: 10px;
      width: 50px;
      height: 50px;
      background: url("@/assets/imgs/logo.jpg");
      background-size: 100% 100%;
    }
  }
  .register-btn {
    font-size: 14px;
    margin-top: 20px;
    color: #f55555;
    border-color: #f55555;
    background: #fff;
  }
  .msg-login {
    font-size: 14px;
    margin-top: 20px;
    color: #f55555;
    border-color: #f55555;
    background: #fff;
  }
}
</style>
